package com.toocms.jinjuhang.binding_adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.ViewFlipper
import androidx.databinding.BindingAdapter

object BindingAdapter {

    @JvmStatic
    @BindingAdapter(value = ["isSelected"])
    fun viewSelectedStatus(view: View, isSelected: Boolean) {
        view.isSelected = isSelected
        if (view is ViewGroup) {
            for (i in 0 until view.childCount) {
                viewSelectedStatus(view.getChildAt(i), isSelected)
            }
        }
    }

    @JvmStatic
    @BindingAdapter(value = ["isEnabled"])
    fun viewEnabledStatus(view: View, isEnabled: Boolean) {
        view.isEnabled = isEnabled
        if (view is ViewGroup) {
            for (i in 0 until view.childCount) {
                viewSelectedStatus(view.getChildAt(i), isEnabled)
            }
        }
    }

    @JvmStatic
    @BindingAdapter(value = ["android:textColor"])
    fun textViewTextColor(textView: TextView, textColorInt: Int) {
        textView.setTextColor(textColorInt)
    }

    @JvmStatic
    @BindingAdapter(value = ["items","itemFactory"])
    fun <T> viewFlipperAdapter(
        viewFlipper: ViewFlipper,
        items: List<T>,
        itemFactory: (context: Context, parent: ViewFlipper, data: T) -> View
    ) {
        viewFlipper.removeAllViews()
        items.forEach { item ->
           val itemView= itemFactory.invoke(viewFlipper.context,viewFlipper,item)
            viewFlipper.addView(itemView)
        }
        viewFlipper.startFlipping()
    }

}